package com.example.vsftpd;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * 添加新用户
 * @author yangjl
 */
@Controller
public class GetSnController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("/getsn")
    public String getsn(HttpServletRequest request){
        String seCode = UUID.randomUUID().toString();
        request.setAttribute("seCode4user", seCode);
        request.getSession().setAttribute("seCode4user", seCode);
        return "getSn";
    }

    @RequestMapping("/generatesn")
    @ResponseBody
    public String generatesn(String upwd, long seCodeNum, HttpServletRequest request){
        Assert.hasText(upwd, "非法访问。");
        if(!"laoyang".equals(upwd)){
            return "kl error.";
        }
        if(seCodeNum<1000){
            return "num error.";
        }
        try {
            String fristText = "Sec1" + (seCodeNum - 10);
            String secondText = "Sec2" + (seCodeNum - 57);
            logger.debug(fristText);
            logger.debug(secondText);
            String se1 = DigestUtils.md5DigestAsHex( fristText.getBytes("GBK") );
            String se2 = DigestUtils.md5DigestAsHex( secondText.getBytes("GBK") );
//            String se1 =org.apache.commons.codec.digest.DigestUtils.md5Hex(fristText);
//            String se2 =org.apache.commons.codec.digest.DigestUtils.md5Hex(secondText);
            String rest = se1.toUpperCase() + se2.toUpperCase();
            logger.debug(rest);
            return rest;
        } catch (Exception e) {
            e.printStackTrace();
            return "error: "+e.getMessage();
        }
    }

}
